// miniprogram/pages/mine/mine.js
import { $wuxDialog } from '../../dist-wux/index'
import { $wuxSelect } from "../../dist-wux/index";

var app = getApp()
var httpUtils = require('../../utils/httpUtils')
var api = require("../../utils/API")
var utils = require("../../utils/utils")
var commonFields = require("../../utils/commonFields")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        wxUserInfo: null,
        isIOS: utils.getStorageSync(commonFields.isIOS),
        shareGif: httpUtils.BASE_URL + "/Content/image/share.gif",
        isOpenBuyVipPage: false,
        items: [
            {
                id: 0,
                title: '个人信息',
                remark: '',
                url: api.PAGES_PERSONALINFORMATION_PERSONALINFORMATION,
                icon: 'md-person',
                show: true,

            },
            {
                id: 1,
                title: '账户信息',
                remark: '',
                url: api.PAGES_ACCOUNT_ACCOUNT,
                icon: 'md-wallet',
                show: false,
            },
            {
                id: 2,
                title: '提现申请',
                remark: '',
                url: api.PAGES_DRAWCASH_DRAWCASH,
                icon: 'md-card',
                show: false,
            },
            {
                id: 3,
                title: '提现订单详情',
                remark: '',
                url: api.PAGES_DRAWCASHORDERDETAIL_DRAWCASHORDERDETAIL,
                icon: 'md-bookmarks',
                show: false,
            },
            {
                id: 4,
                title: '我的学员',
                remark: '',
                url: api.PAGES_MYSTU_MYSTU,
                icon: 'md-people',
                show: false,
            },
            {
                id: 5,
                title: '考试记录',
                remark: '',
                icon: 'md-paper',
                url: api.PAGES_EXAMRECORDS_EXAMRECORDS,
                show: true,
            },
            {
                id: 6,
                title: '我的二维码',
                remark: '',
                icon: 'md-qr-scanner',
                url: api.PAGES_MYQRCODE_MYQRCODE,
                show: true,
            },
        ]
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            isIOS: utils.getStorageSync(commonFields.isIOS),
            isOpenBuyVipPage: utils.getStorageSync(commonFields.isOpenBuyVipPage),
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        let wxUserInfo = utils.getStorageSync(commonFields.wxUserInfo)
        if (wxUserInfo) {
            httpUtils.postUserInfo(

                (res, statusCode) => {
                    console.log("=====>>>>", res, "状态码", statusCode)
                    if (statusCode == 401) {
                        that.getUserInfo()
                        that.onClickAvatar()
                        // utils.elementClick("userinfo-avatar")
                    } else if (res.data) {
                        utils.setStorageSync(commonFields.userInfo, res.data)
                        console.log("生命周期函数 UserInfo", res.data)
                        that.setData({
                            userInfo: res.data,
                        })

                        console.log("that.data.items.length", that.data.items.length)
                        for (let i = 1; i < that.data.items.length - 1; i++) {
                            let itemField = 'items[' + i + '].show'
                            that.setData({
                                [itemField]: res.data && res.data.userType == 2
                            })
                        }
                    }
                })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    getUserInfo() {
        // return new Promise(function (resolve, reject) {
        // wx.getSetting({
        //     success(res) {
        //         let isScopeUserInfo = res.authSetting['scope.userInfo'];
        //         console.log("app getUserInfo isScopeUserInfo", isScopeUserInfo)
        //         if (!isScopeUserInfo) {
        // wx.authorize({
        //     scope: 'scope.userInfo',
        //     success() {
        // 用户已经同意小程序使用录音功能，后续调用 wx.getUserInfo 接口不会弹窗询问
        // 必须是在用户已经授权的情况下调用
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success(res) {
                console.log("app getUserProfile", res.userInfo)
                utils.setStorageSync(commonFields.wxUserInfo, res.userInfo)

            }
        })
        // },
        // fail(err) {
        //     console.log("app getUserInfo is error", err)
        //     reject(err)
        // }
        // })
        //         } else {
        //             console.log("app auth userInfo 已经有权限了")
        //         }
        //     }
        // })
        // })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let userInfo = utils.getStorageSync(commonFields.userInfo);
        let shareCode = null;
        if (userInfo && userInfo.shareCode) {
            console.log("我的分享码:", userInfo.shareCode)
            shareCode = userInfo.shareCode
        }
        console.log("我的分享码:", shareCode)
        let qrcode = utils.getStorageSync(commonFields.shareImg)
        return {

            title: '升级版最新理论题库，精简题，技巧答题，语音讲解，简单易学，快速易懂，包你考试轻轻松松过',

            desc: '升级版最新理论题库，精简题，技巧答题，语音讲解，简单易学，快速易懂，包你考试轻轻松松过',

            imageUrl: httpUtils.BASE_URL + "/Content/image/share_url.jpg",

            path: shareCode ? api.PAGES_MOCKEXAMS_MOCKEXAMS + '?shareCode=' + shareCode : api.PAGES_MOCKEXAMS_MOCKEXAMS, // 路径，传递参数到指定页面。

        }
    },

    onGetUserInfo: function (e) {

        this.getUserInfo()
        this.onClickAvatar()

    },

    /**
     * 点击
     * @param e
     */
    onClickBuyVip(e) {
        console.log(e)
        wx.navigateTo({
            url: api.PAGES_BUYVIP_BUYVIP,
        })
    },


    onClickItem(e) {
        console.log(e)
    },


    onClickAvatar(e) {
        console.log("onClickAvatar===>", e)
        let that = this;
        $wuxDialog().open({
            resetOnClose: true,
            title: '提示',
            content: '选择登录方式',
            verticalButtons: !0,
            buttons: [
                {
                    text: '微信登录',
                    bold: !0,
                    onTap(e) {
                        that.loginByWechat()
                    },
                },
                {
                    text: '手机登录',
                    bold: !0,
                    onTap(e) {
                        wx.navigateTo({
                            url: api.PAGES_LOGIN_LOGIN
                        })
                    },
                },
                {
                    text: '取消',
                    bold: !0,
                },
            ],
        })
    },

    /**
     * 微信登录
     */
    loginByWechat() {
        let that = this;
        httpUtils.postRequest(api.API_USER_AUTH,
            {
                "openid": utils.getStorageSync(commonFields.openid),
                "shareCode": utils.getStorageSync(commonFields.shareCode),
                "wxName": utils.getStorageSync(commonFields.wxUserInfo).nickName,
                "wxHeadImg": utils.getStorageSync(commonFields.wxUserInfo).avatarUrl,
            },
            res => {
                console.log("微信登录信息", res.data)
                utils.setStorageSync(commonFields.userInfo, res.data)
                this.setData({
                    isLogin: true,
                    userInfo: res.data,
                    nickName: utils.getStorageSync(commonFields.wxUserInfo).nickName,
                    userAvatar: utils.getStorageSync(commonFields.wxUserInfo).avatarUrl,
                })
                console.log("that.data.items.length", that.data.items.length)
                for (let i = 1; i < that.data.items.length - 1; i++) {
                    let itemField = 'items[' + i + '].show'
                    that.setData({
                        [itemField]: res.data && res.data.userType == 2
                    })
                }
                wx.showToast({
                    title: '登录成功',
                    icon: 'success',
                    duration: 1500
                })
                setTimeout(() => {
                    if (!utils.getStorageSync(commonFields.isIOS) && utils.getStorageSync(commonFields.isOpenBuyVipPage)) {
                        wx.navigateTo({
                            url: api.PAGES_BUYVIP_BUYVIP
                        })
                    }
                }, 1500)
            },
            fail => {
                console.log(fail)
            }
        );
    },

    /**
     * 呼叫客服
     * @param e
     */
    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: api.callPhone,
        })
    },
})
/*

{
    cash:0
    isvip:false
    mobile:"18971414591"
    openid:"o0hRH40Gc_bbSmd-oDQaIAnr5_Wo"
    status:1
    token:"C28E4083D5350E6761A6300C9E1BDCCF3BBF75914D491620C9EC8B9E20912C2D7FA876015730F374FEDE8A792A622F60670C950E24E325B464398ABF0433F4984D97E48F0F8647953BEF4F458CA15B293FB337C039F0D093B14509E912C04C38106312D8BDD35A34B9FFDE3EC45EF30A96D03CAE648070809DA0946044E3C117D4D61EBA007CF4EDE91C2D501E03F8BCA21935117D8B601D214F183F1929A8F806044923298C77E6C20C6B93D55FE3FF09CDB3F8425B1D411E57953DC951DB9F030FE5F038157AEC8F34FFD568FEACD26CF16D5ED1F01BB985ECE48976F1601709957011B554E0E25DC1A0D2344E6AD8E8F10AF5F15019A7683572B2CB3636FB3A9BE7BB1DFCFC7CC08DF609DBDA302F"
    userType: 1
    vipEnd:""
    vipStart :""
    webName:null
    wxHeadImg: "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLDhrlrSzL4fOicOibXhrXIO5TAt5QV4z3Xia0TCqIKf8433ltw8dFZAk6Lb6PUbR6ydN6ric3qFcYvBQ/132"
}
*/
